using UnityEngine;
using System.Collections;

/// \class  gkLifeComponent
/// \brief  Store the submarine life
[AddComponentMenu("GK/Engine/Game/Life/gkLifeComponent")]
public class gkLifeComponent : MonoBehaviour 
{
	/// The begin life
	public int BeginLife = 3;
	
	/// The max life
	public int MaxLife = 3;
	
	/// The life
	private int m_iLife;

	/// is the owner dead?
	private bool m_bDead;
	
	/// Is the owner is invincible?
	private bool m_bInvincibility;
	
	/// \brief  Called just after the game begin to play
	void Start()
	{
		m_iLife = BeginLife;
	}
	
	/// \brief  Get the current score
	public int GetLife()
	{
		return m_iLife;
	}

	/// \brief  Decrease life
	public void DecreaseLife(int a_iNbLifeLost = 1)
	{
		if(m_bInvincibility == false)
		{
			// Decrease life
			m_iLife -= a_iNbLifeLost;
			
			// Notify of the life loss
			NotifyLifeLoss();
			
			// Check for death
			if(m_iLife <= 0)
			{
				m_iLife = 0;
				if(m_bDead == false)
				{
					m_bDead = true;
					NotifyDeath();
				}
			}
		}
	}

	/// \brief  Decrease life
	public void IncreaseLife(int a_iNbLifeAdded = 1)
	{
		m_iLife += a_iNbLifeAdded;
		if(m_iLife > MaxLife)
		{
			m_iLife = MaxLife;
		}
	}
	
	/// \brief  Set if the owner is invicible or not
	public void SetInvincibility(bool a_bInvincibility)
	{
		m_bInvincibility = a_bInvincibility;
	}
	
	/// \brief  Notify the death of the owner
	void NotifyDeath()
	{
		GetComponent<gkEventComponent>().RaiseEvent(gkLifeEvent.Death);
	}
	
	/// \brief  Notify the life loss of the owner
	void NotifyLifeLoss()
	{
		GetComponent<gkEventComponent>().RaiseEvent(gkLifeEvent.LifeLoss);
	}
}
